/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.cardapiodigital.model;

import br.com.cardapiodigital.data.DestaquesData;
import br.com.cardapiodigital.data.EstabelecimentosData;
import br.com.cardapiodigital.entity.Destaques;
import br.com.cardapiodigital.entity.Estabelecimentos;
import br.com.cardapiodigital.exception.CardapioDigitalException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author egberto
 */
@ManagedBean
public class DestaquesModel extends AbstractModel implements Serializable {
      
    private DestaquesData destaquesData;
    private Destaques destaques = null;
    private List<Destaques> todosDestaques = null;
    
    //estabelecimento
    
    private int estabSelecionado;
    private EstabelecimentosData estabData = null;  
    
   
    
    public DestaquesModel(){}
    
    public void salvar() {
        try {                

            getDestaques().setEstabelecimentosId(getEstabData().obtem(estabSelecionado));            
            getDestaquesData().salvar(getDestaques());            
            closeDialog();
            displayInfoMessageToUser("Created With Sucess");
            carregarTodosDestaques();
            resetDestaque();
        } catch (Exception e) {
            keepDialogOpen();
            displayErrorMessageToUser("Ops, we could not create. Try again later");
            e.printStackTrace();
        }
    }
    
    public void atualizar(){
        try {
            getDestaquesData().atualizar(getDestaques());
            closeDialog();
            displayInfoMessageToUser("Updated With Sucess");
            carregarTodosDestaques();
            resetDestaque();
        } catch (Exception e) {
            keepDialogOpen();
            displayErrorMessageToUser("Ops, we could not create. Try again later");
            e.printStackTrace();
        }
    }

    public DestaquesData getDestaquesData() {
        if(destaquesData == null){
            destaquesData = new DestaquesData();
        }
        return destaquesData;
    }

    public Destaques getDestaques() {
        if(destaques == null){
            destaques = new Destaques();
        }
        return destaques;
    }

    public void setDestaques(Destaques destaques) {
        this.destaques = destaques;
    }

    public List<Destaques> carregarTodosDestaques() throws CardapioDigitalException {
        return todosDestaques = getDestaquesData().buscarTodos();
    }
    
     public void resetDestaque() {
        destaques = new Destaques();
    }

    public int getEstabSelecionado() {
        
        return estabSelecionado;
    }

    public void setEstabSelecionado(int estabSelecionado) {
        this.estabSelecionado = estabSelecionado;
    }

    public EstabelecimentosData getEstabData() {
        if(estabData == null){
            estabData = new EstabelecimentosData();
        }
        return estabData;
    }   
    
    
        public void processFileUpload(FileUploadEvent event) throws IOException {  
            try {  
                UploadedFile arq = event.getFile();  
                InputStream in = new BufferedInputStream(arq.getInputstream());  
                File file = new File("D://fatec//inter//jdbc//images//" + arq.getFileName());  
      
                FileOutputStream fout = new FileOutputStream(file);  
      
                while (in.available() != 0) {  
                    fout.write(in.read());  
                }  
                fout.close();  
                FacesMessage msg = new FacesMessage("O Arquivo ", file.getName()  
                        + " salvo.");  
                FacesContext.getCurrentInstance().addMessage("msgUpdate", msg);  
            }  
            catch (Exception ex) {  
                ex.printStackTrace();  
            }  
        }  

    
     
    
    
    
}
